home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / cawf404.zip / regexp.c < prev    next >
C/C++ Source or Header  |  1993-12-07  |  31KB  |  1,243 lines

  1. /*
  2.  * regcomp and regexec -- regsub and regerror are elsewhere
  3.  *
  4.  *    Copyright (c) 1986 by University of Toronto.
  5.  *    Written by Henry Spencer.  Not derived from licensed software.
  6.  *
  7.  *    Permission is granted to anyone to use this software for any
  8.  *    purpose on any computer system, and to redistribute it freely,
  9.  *    subject to the following restrictions:
  10.  *
  11.  *    1. The author is not responsible for the consequences of use of
  12.  *        this software, no matter how awful, even if they arise
  13.  *        from defects in it.
  14.  *
  15.  *    2. The origin of this software must not be misrepresented, either
  16.  *        by explicit claim or by omission.
  17.  *
  18.  *    3. Altered versions must be plainly marked as such, and must not
  19.  *        be misrepresented as being the original software.
  20.  *
  21.  * Beware that some of this code is subtly aware of the way operator
  22.  * precedence is structured in regular expressions.  Serious changes in
  23.  * regular-expression syntax might require a total rethink.
  24.  */
  25. #include <stdio.h>
  26. #ifdef    UNIX
  27. #ifdef    USG
  28. #include <string.h>
  29. #else
  30. #include <strings.h>
  31. #endif
  32. #else
  33. #include <string.h>
  34. #endif
  35. #include "regexp.h"
  36. #include "regmagic.h"
  37. #include "proto.h"
  38.  
  39. /*
  40.  * The "internal use only" fields in regexp.h are present to pass info from
  41.  * compile to execute that permits the execute phase to run lots faster on
  42.  * simple cases.  They are:
  43.  *
  44.  * regstart    char that must begin a match; '\0' if none obvious
  45.  * reganch    is the match anchored (at beginning-of-line only)?
  46.  * regmust    string (pointer into program) that match must include, or NULL
  47.  * regmlen    length of regmust string
  48.  *
  49.  * Regstart and reganch permit very fast decisions on suitable starting points
  50.  * for a match, cutting down the work a lot.  Regmust permits fast rejection
  51.  * of lines that cannot possibly match.  The regmust tests are costly enough
  52.  * that regcomp() supplies a regmust only if the r.e. contains something
  53.  * potentially expensive (at present, the only such thing detected is * or +
  54.  * at the start of the r.e., which can involve a lot of backup).  Regmlen is
  55.  * supplied because the test in regexec() needs it and regcomp() is computing
  56.  * it anyway.
  57.  */
  58.  
  59. /*
  60.  * Structure for regexp "program".  This is essentially a linear encoding
  61.  * of a nondeterministic finite-state machine (aka syntax charts or
  62.  * "railroad normal form" in parsing technology).  Each node is an opcode
  63.  * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
  64.  * all nodes except BRANCH implement concatenation; a "next" pointer with
  65.  * a BRANCH on both ends of it is connecting two alternatives.  (Here we
  66.  * have one of the subtle syntax dependencies:  an individual BRANCH (as
  67.  * opposed to a collection of them) is never concatenated with anything
  68.  * because of operator precedence.)  The operand of some types of node is
  69.  * a literal string; for others, it is a node leading into a sub-FSM.  In
  70.  * particular, the operand of a BRANCH node is the first node of the branch.
  71.  * (NB this is *not* a tree structure:  the tail of the branch connects
  72.  * to the thing following the set of BRANCHes.)  The opcodes are:
  73.  */
  74.  
  75. /* definition    number    opnd?    meaning */
  76. #define    END    0    /* no    End of program. */
  77. #define    BOL    1    /* no    Match "" at beginning of line. */
  78. #define    EOL    2    /* no    Match "" at end of line. */
  79. #define    ANY    3    /* no    Match any one character. */
  80. #define    ANYOF    4    /* str    Match any character in this string. */
  81. #define    ANYBUT    5    /* str    Match any character not in this string. */
  82. #define    BRANCH    6    /* node    Match this alternative, or the next... */
  83. #define    BACK    7    /* no    Match "", "next" ptr points backward. */
  84. #define    EXACTLY    8    /* str    Match this string. */
  85. #define    NOTHING    9    /* no    Match empty string. */
  86. #define    STAR    10    /* node    Match this (simple) thing 0 or more times. */
  87. #define    PLUS    11    /* node    Match this (simple) thing 1 or more times. */
  88. #define    OPEN    20    /* no    Mark this point in input as start of #n. */
  89.             /*    OPEN+1 is number 1, etc. */
  90. #define    CLOSE    30    /* no    Analogous to OPEN. */
  91.  
  92. /*
  93.  * Opcode notes:
  94.  *
  95.  * BRANCH    The set of branches constituting a single choice are hooked
  96.  *        together with their "next" pointers, since precedence prevents
  97.  *        anything being concatenated to any individual branch.  The
  98.  *        "next" pointer of the last BRANCH in a choice points to the
  99.  *        thing following the whole choice.  This is also where the
  100.  *        final "next" pointer of each individual branch points; each
  101.  *        branch starts with the operand node of a BRANCH node.
  102.  *
  103.  * BACK        Normal "next" pointers all implicitly point forward; BACK
  104.  *        exists to make loop structures possible.
  105.  *
  106.  * STAR,PLUS    '?', and complex '*' and '+', are implemented as circular
  107.  *        BRANCH structures using BACK.  Simple cases (one character
  108.  *        per match) are implemented with STAR and PLUS for speed
  109.  *        and to minimize recursive plunges.
  110.  *
  111.  * OPEN,CLOSE    ...are numbered at compile time.
  112.  */
  113.  
  114. /*
  115.  * A node is one char of opcode followed by two chars of "next" pointer.
  116.  * "Next" pointers are stored as two 8-bit pieces, high order first.  The
  117.  * value is a positive offset from the opcode of the node containing it.
  118.  * An operand, if any, simply follows the node.  (Note that much of the
  119.  * code generation knows about this implicit relationship.)
  120.  *
  121.  * Using two bytes for the "next" pointer is vast overkill for most things,
  122.  * but allows patterns to get big without disasters.
  123.  */
  124. #define    OP(p)    (*(p))
  125. #define    NEXT(p)    (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
  126. #define    OPERAND(p)    ((p) + 3)
  127.  
  128. /*
  129.  * See regmagic.h for one further detail of program structure.
  130.  */
  131.  
  132.  
  133. /*
  134.  * Utility definitions.
  135.  */
  136. #ifndef CHARBITS
  137. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  138. #else
  139. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  140. #endif
  141.  
  142. #define    FAIL(m)    { regerror(m); return(NULL); }
  143. #define    ISMULT(c)    ((c) == '*' || (c) == '+' || (c) == '?')
  144. #define    META    "^$.[()|?+*\\"
  145.  
  146. /*
  147.  * Flags to be passed up and down.
  148.  */
  149. #define    HASWIDTH    01    /* Known never to match null string. */
  150. #define    SIMPLE        02    /* Simple enough to be STAR/PLUS operand. */
  151. #define    SPSTART        04    /* Starts with * or +. */
  152. #define    WORST        0    /* Worst case. */
  153. #define STATIC 
  154. /*
  155.  * Global work variables for regcomp().
  156.  */
  157.  
  158. STATIC char *regparse;        /* Input-scan pointer. */
  159. STATIC int regnpar;        /* () count. */
  160. STATIC unsigned char regdummy;
  161. STATIC unsigned char *regcode;    /* Code-emit pointer; ®dummy = don't. */
  162. STATIC long regsize;        /* Code size. */
  163.  
  164. /*
  165.  * Forward declarations for regcomp()'s friends.
  166.  */
  167. _PROTOTYPE(STATIC unsigned char *reg, (int paren, int *flagp ));
  168. _PROTOTYPE(STATIC unsigned char *regbranch, (int *flagp ));
  169. _PROTOTYPE(STATIC unsigned char *regpiece, (int *flagp ));
  170. _PROTOTYPE(STATIC unsigned char *regatom, (int *flagp ));
  171. _PROTOTYPE(STATIC unsigned char *regnode, (int op ));
  172. _PROTOTYPE(STATIC void regc, (int b ));
  173. _PROTOTYPE(STATIC void reginsert, (int op, unsigned char *opnd ));
  174. _PROTOTYPE(STATIC void regtail, (unsigned char *p, unsigned char *val ));
  175. _PROTOTYPE(STATIC void regoptail, (unsigned char *p, unsigned char *val ));
  176. _PROTOTYPE(STATIC int regtry, (regexp *prog, unsigned char *string ));
  177. _PROTOTYPE(STATIC int regmatch, (unsigned char *prog ));
  178. _PROTOTYPE(STATIC int regrepeat, (unsigned char *p ));
  179. _PROTOTYPE(STATIC unsigned char *regnext, (unsigned char *p ));
  180. _PROTOTYPE(STATIC unsigned char *regprop, (unsigned char *op ));
  181.  
  182. #ifdef STRCSPN
  183. _PROTOTYPE(STATIC int strcspn, (unsigned char *s1, unsigned char *s2 ));
  184. #endif
  185.  
  186. /*
  187.  - regcomp - compile a regular expression into internal code
  188.  *
  189.  * We can't allocate space until we know how big the compiled form will be,
  190.  * but we can't compile it (and thus know how big it is) until we've got a
  191.  * place to put the code.  So we cheat:  we compile it twice, once with code
  192.  * generation turned off and size counting turned on, and once "for real".
  193.  * This also means that we don't allocate space until we are sure that the
  194.  * thing really will compile successfully, and we never have to move the
  195.  * code and thus invalidate pointers into it.  (Note that it has to be in
  196.  * one piece because free() must be able to free it all.)
  197.  *
  198.  * Beware